<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js内建对象数组常见操作</title>
</head>
<body>

<script>
    var arr1 = [1, 2, 3]
    var arr2 = [4, 3, 5, 6, 7, 3]

    // 合并数组
    console.log('-------------合并数组concat--------------')
    // var result =  arr2.concat(arr1)
    // es6  解构赋值
    var result = [...arr1, ...arr2]
    console.log(result)

    console.log('-------------填充数组 fill--------------')
    var arr2 = new Array(5)
    arr2.fill('1')
    console.log(arr2)


    console.log('-------------过滤数组中的元素 filter--------------')
    // function fn(num){
    //     return num%2==0
    // }
    var new_result = result.filter(val => val % 2 == 0)   //   布尔类型  true 通过  false 拦截
    console.log(new_result)

    console.log('-------------判定元素在数组中第一次出现的位置 indexOf--------------')
    console.log(result)

    var index = result.indexOf(3)
    console.log('index = ', index)
    // 参数2  表示从哪个位置开始查找
    index = result.indexOf(3, index + 1)
    console.log('index = ', index)

    index = result.lastIndexOf(3)
    console.log('lastIndex = ', index)

    console.log('-------------添加元素 push  unshift--------------')
    result.push('admin')
    console.log(result)
    result.unshift('super')
    console.log(result)

    console.log('-------------删除元素 pop shift--------------')
    console.log(result)
    var ele = result.pop()
    console.log(ele)
    console.log(result)

    ele = result.shift()
    console.log(ele)
    console.log(result)

    console.log('-------------添加或删除元素splice--------------')
    // 参数1  start   参数2  删除元素的个数  参数3  在删除过元素的位置添加新元素
    ele = result.splice(2, 3, 'first', 'second')
    console.log(ele)
    console.log(result)

    console.log('-------------翻转数组--------------')
    result = [1,2,10,20,5]
    result.reverse()
    console.log(result)
    console.log('-------------数组排序--------------')
    //  传递排序函数  实现数字排序
    result.sort((num1,num2)=> num2 - num1)
    console.log(result)



</script>

</body>
</html>